package com.ppgames.gm.gmsystem.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 主数据源配置
 *
 * @author Joeg
 * @email 840459673@qq.com
 * @date 2018/4/19
 */

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.ppgames.gm.gmsystem.mapper.db1"}, sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DB1DataSourceConfig {


    /**
     * druid 数据源配置
     *
     * @param driver
     * @param url
     * @param username
     * @param password
     * @param minIdle
     * @param initialSize
     * @param maxActive
     * @param testWhileIdle
     * @param validationQuery
     * @param betweenMillis
     * @param filters
     * @param connectionProperties
     * @return
     */
    @Primary
    @Bean(name = "dataSource_1")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource dataSource(@Value("${spring.datasource.db1.driver-class-name}") String driver,
                                 @Value("${spring.datasource.db1.url}") String url,
                                 @Value("${spring.datasource.db1.username}") String username,
                                 @Value("${spring.datasource.db1.password}") String password,
                                 @Value("${spring.datasource.db1.druid.minIdle}") int minIdle,
                                 @Value("${spring.datasource.db1.druid.initialSize}") int initialSize,
                                 @Value("${spring.datasource.db1.druid.maxActive}") int maxActive,
                                 @Value("${spring.datasource.db1.druid.test-while-idle}") boolean testWhileIdle,
                                 @Value("${spring.datasource.db1.druid.validation-query}") String validationQuery,
                                 @Value("${spring.datasource.db1.druid.time-between-eviction-runs-millis}") int betweenMillis,
                                 @Value("${spring.datasource.db1.druid.filters}") String filters,
                                 @Value("${spring.datasource.db1.druid.connectionProperties}") String connectionProperties,
                                 @Value("${spring.datasource.db1.druid.keepAlive}") boolean keepAlive) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setMinIdle(minIdle);
        druidDataSource.setInitialSize(initialSize);
        druidDataSource.setMaxActive(maxActive);
        druidDataSource.setTestWhileIdle(testWhileIdle);
        druidDataSource.setTimeBetweenEvictionRunsMillis(betweenMillis);
        druidDataSource.setValidationQuery(validationQuery);
        druidDataSource.setKeepAlive(keepAlive);

        System.out.println("1");
        try {
            druidDataSource.setFilters(filters);
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
        }
        druidDataSource.setConnectionProperties(connectionProperties);
        return druidDataSource;
    }


    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSource_1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/db1/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("dataSource_1") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    /*

    @Primary
    @Bean(name = "entityManagerFactory_1")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource_1") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.ppgames.gm.gmsystem.controller")
                .persistenceUnit("default")
                .build();
    }

//    @Primary
    @Bean(name = "transactionManager_1")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory_1") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
*/


}
